
clear all
set more off
cap log close
version 13
local gruppo = 1

log  using  /ssb/stamme04/ekstern/apa/prog/apa/survival/revision/New_runs/Sept_runs/Panel_B2/A_4_`gruppo'_2_13Sep2020.smcl, replace


// global paths 
global datainpath "..."
global dataoutpath "..."
global graphpath "..."
global outputpath "..."

global wijioutput "..."



use "${dataoutpath}left_cens_spell_10sample_all_4type_`gruppo'_rev1_2_13Sep2020.dta", clear    /* changed 13 June 2019 */
drop if ban_`gruppo' == 1

foreach m of newlist lm_status lcensor lcens_id lcens_spell lastsp_unem ban a_spell ///
    a_seq a_end dur last_dur_yr last_yr_smpl last_yr  r_c durvar sedum {
rename `m'_`gruppo' `m'
*drop `m'_*
}

set more off
gen kids = (famtype ==3 | famtype==4 | famtype==5 | famtype == 6)
gen earnings= wage if lm_status==2
replace earnings=net_naering if lm_status==1
gen lnearnings=ln(earnings)
gen  wageearners=(1-sedum)
*** gen age squared
gen agesquared=age^2
gen age3=age^3

***** gen regions
gen region=0
replace region=1 if county>=1 & county<=8
replace region=2 if county>8 & county<=10
replace region=3 if county>10 & county<=15
replace region=4 if county>15 & county<=17
replace region=5 if county>17 & county<=20

forvalues m = 1(1)5 {
	gen region_`m'=0
	replace region_`m' = 1 if (region==`m')
}
*movestay lnearnings age agesquared region_2-region_5 field2-field10 if year==2007 & group_max_lev==3 & max_lev_edu==lev_educ ,select(wageearners=kids net_wealth age agesquared region_2-region_5 field2-field10)  
gen lnearnings1 = lnearnings if sedum==0
*replace lnearnings1 =0 if missing(lnearnings1)
gen lnearnings2 = lnearnings if sedum==1
*replace lnearnings2 =0 if missing(lnearnings2)


gen lol=(sedum==1 & lnearnings2==.)

preserve

global xlist age agesquared region_2-region_5 educ_1_3-educ_1_8 educ_2_2-educ_2_8 educ_3_2-educ_3_11 female
global inslist kids head married

keep if (year==1993)   


*** Heckman MLE
   heckman lnearnings2 $xlist if lol==0  , select(sedum= $inslist $xlist)  
      outreg2 using "${wijioutput}AA_4_wage_eq.xls",  replace ctitle(1993 SE ) par(se) sdec(2) bdec(3) noaster

   predict predlnnaeringlambda, ycond
   gen lambdanear= e(lambda) 
   predict millsnaer, mills
   ** for convexity I need this stuff as well
   gen sigma_naer=e(sigma)  
   gen res_naer= ln((lnearnings2-predlnnaeringlambda)^2) if  sedum   & lol==0
   reg res_naer $inslist $xlist
     outreg2 using "${wijioutput}AA_4_wage_eq.xls",  append ctitle(1993_SE_het) par(se) sdec(2) bdec(3) noaster

   predict h_hat_naer
   replace h_hat_naer=exp(h_hat_naer)   
 
   ** wage heckman  
   heckman lnearnings1 $xlist if lol==0, select(wageearners= $inslist  $xlist) 
	outreg2 using "${wijioutput}AA_4_wage_eq.xls",  append ctitle(1993_WE) par(se) sdec(2) bdec(3) noaster

   gen lambdawage= e(lambda)
   predict predlnwagelambda ,ycond
   predict millswage , mills

   ** for convexity I need this stuff as well
   gen sigma_wage=e(sigma)  
   gen res_wage= ln((lnearnings1-predlnwagelambda)^2) if  wageearn 
   reg res_wage $inslist $xlist
      outreg2 using "${wijioutput}AA_4_wage_eq.xls",  append ctitle(1993_WE) par(se) sdec(2) bdec(3) noaster

   predict h_hat_wage
   replace h_hat_wage=exp(h_hat_wage) 
 
   **replace prediciton for counterfactual of wage and naering assuming mills=-()/()
   replace  predlnnaeringlambda=predlnnaeringlambda-lambdanear*(millsnaer+millswage) if wageearn
   replace  predlnwagelambda=predlnwagelambda-lambdawage*(millsnaer+millswage) if sedum 
   

bys sedum: su  pred* h_hat* lambda* lnearnings*  mills*  sigma*, sep(0)

save ${dataoutpath}left_cens_spell_10sample_10never_type_`gruppo'_rev1_2_13Sep2020.dta, replace
                   


***************************************************************************
*** Now we do 1994 onwards 
***************************************************************************

restore
drop if year == 1993

levelsof year, local(levels) 

foreach l of local levels { 
preserve
keep if (year==`l')
 
*** Heckman MLE
   heckman lnearnings2 $xlist if lol==0  , select(sedum= $inslist $xlist)  
      outreg2 using "${wijioutput}AA_4_wage_eq.xls",  append ctitle(`l' SE ) par(se) sdec(2) bdec(3) noaster

   predict predlnnaeringlambda, ycond
   gen lambdanear= e(lambda) 
   predict millsnaer, mills
   ** for convexity I need this stuff as well
   gen sigma_naer=e(sigma)  
   gen res_naer= ln((lnearnings2-predlnnaeringlambda)^2) if  sedum   & lol==0
   reg res_naer $inslist $xlist
     outreg2 using "${wijioutput}AA_4_wage_eq.xls",  append ctitle(`l'_SE_het) par(se) sdec(2) bdec(3) noaster

   predict h_hat_naer
   replace h_hat_naer=exp(h_hat_naer)   
 
   ** wage heckman  
   heckman lnearnings1 $xlist if lol==0, select(wageearners= $inslist  $xlist) 
	outreg2 using "${wijioutput}AA_4_wage_eq.xls",  append ctitle(`l'_WE) par(se) sdec(2) bdec(3) noaster

   gen lambdawage= e(lambda)
   predict predlnwagelambda ,ycond
   predict millswage , mills

   ** for convexity I need this stuff as well
   gen sigma_wage=e(sigma)  
   gen res_wage= ln((lnearnings1-predlnwagelambda)^2) if  wageearn 
   reg res_wage $inslist $xlist
      outreg2 using "${wijioutput}AA_4_wage_eq.xls",  append ctitle(`l'_WE) par(se) sdec(2) bdec(3) noaster

   predict h_hat_wage
   replace h_hat_wage=exp(h_hat_wage) 
 
   **replace prediciton for counterfactual of wage and naering assuming mills=-()/()
   replace  predlnnaeringlambda=predlnnaeringlambda-lambdanear*(millsnaer+millswage) if wageearn
   replace  predlnwagelambda=predlnwagelambda-lambdawage*(millsnaer+millswage) if sedum 
   
	bys sedum: su  pred* h_hat* lambda* lnearnings*  mills*  sigma*, sep(0)
   

append using ${dataoutpath}left_cens_spell_10sample_10never_type_`gruppo'_rev1_2_13Sep2020.dta

save ${dataoutpath}left_cens_spell_10sample_10never_type_`gruppo'_rev1_2_13Sep2020.dta, replace

restore
}

use ${dataoutpath}left_cens_spell_10sample_10never_type_`gruppo'_rev1_2_13Sep2020.dta, clear 

gen everself1=lm_status==1
bysort id: egen everself1sum=max(everself1)

bys id: gen d=_n==1
tab everself1sum if d==1

sort id
capture drop _merge
preserve
tempfile tmp
bysort id: keep if _n == 1
set seed 12
sample 10 if everself1sum==0
sort id
save `tmp'
restore
merge m:1 id using `tmp'
keep if _merge == 3
drop _merge 

drop everself1 everself1sum d

save ${dataoutpath}left_cens_spell_10sample_10never_type_`gruppo'_rev1_2_13Sep2020.dta, replace



log close
